﻿/**************************************************************************
创建时间:	2020/9/8 22:46:37    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using log4net;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using ZhCun.Utils;
using ZhCun.WebUtils.Configs;

namespace ZhCun.WebUtils
{
    static class HttpMonitor
    {
        static HttpMonitor()
        {
            Init();
        }

        static object LockInitObject = new object();

        public static void Init()
        {
            if (_Log == null)
            {
                lock (LockInitObject)
                {
                    if (_Log == null)
                    {
                        _Log = LogHelper.CreateLog("HttpLog");
                        _IsMonitor = CommonConfig.THIS.MonitorHttp;
                        _Log.Info($"开启监听HTTP,{_IsMonitor}");
                    }
                }
            }
        }

        static ILog _Log;

        static bool _IsMonitor;

        //public static void Login(HttpContent content)
        //{ 

        //}

        public static void OnRequest(HttpRequest request)
        {
            if (!_IsMonitor) return;

            string log = $"[REQ][{request.HttpContext.GetRemoteIp()}][{request.HttpContext.Connection.RemotePort}][{request.Method}][{request.Path}][{request.ContentLength}][{request.ContentType}]";
            _Log.Info(log);
            LogHelper.LogObj.Info(log);
        }

        public static void OnResponse(HttpResponse response)
        {
            if (!_IsMonitor) return;

            string log = $"[REP][{response.StatusCode}][{response.ContentLength}][{response.ContentType}]";
            _Log.Info(log);
            LogHelper.LogObj.Info(log);
        }
    }
}
